package com.treasure.infrastructure;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * @author sunhongmin
 * @date 2023/8/31 20:22
 * @description
 */
public class MybatisPlusGeneratorUtil {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:postgresql://localhost:5432/postgres", "postgres", "password")
                .globalConfig(builder -> {
                    builder.author("sunhongmin") // 设置作者
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("/Users/sunhongmin/IdeaProjects/ai-treasure-chest/treasure-infrastructure/src/main/java/com/treasure/infrastructure/repository"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.treasure.infrastructure.repository") // 设置父包名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "/Users/sunhongmin/IdeaProjects/ai-treasure-chest/treasure-infrastructure/src/main/resources/mapper2/")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder
//                            .addInclude("corp_email_account")
//                            .addInclude("corp_info")
//                            .addInclude("corp_trial_approve")
//                            .addInclude("corp_user_info")
//                            .addInclude("knowledge_document")// 设置需要生成的表名
//                            .addInclude("account")// 设置需要生成的表名
//                            .addInclude("corp_role_info")// 设置需要生成的表名
//                            .addInclude("user_role_mapping")// 设置需要生成的表名
//                            .addInclude("knowledge_base")// 设置需要生成的表名
//                            .addInclude("knowledge_document")// 设置需要生成的表名
//                            .addInclude("knowledge_document_vector")// 设置需要生成的表名
//                            .addInclude("ai_robot")// 设置需要生成的表名
//                            .addInclude("robot_setting")// 设置需要生成的表名
//                            .addInclude("chat_history")// 设置需要生成的表名
//                            .addInclude("chat_history_detail")// 设置需要生成的表名
//                            .addInclude("corp_department_info")// 设置需要生成的表名
//                            .addInclude("corp_access_token")// 设置需要生成的表名
//                            .addInclude("conversation_details")// 设置需要生成的表名
                            .addInclude("model_usage_details")// 设置需要生成的表名
//                            .addInclude("user_ratings")// 设置需要生成的表名
//                            .addInclude("sms_verify_code_info")// 设置需要生成的表名
                    ; // 设置过滤表前缀
                })
                .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
